﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NTwitter;
using System.Collections.ObjectModel;
using System.Drawing;

namespace TwitterClient2.Components
{
    class TabPageTransmittedReceived : TabPage
    {
        private VScrollBar vScrollBar;
        private List<PanelMessage> messagelist = new List<PanelMessage>();
        private int messageLength = 0;

        public TabPageTransmittedReceived()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.vScrollBar = new System.Windows.Forms.VScrollBar();
            this.SuspendLayout();
            // 
            // vScrollBar
            // 
            this.vScrollBar.Location = new System.Drawing.Point(210, 0);
            this.vScrollBar.Name = "vScrollBar";
            this.vScrollBar.Size = new System.Drawing.Size(10, 240);
            this.vScrollBar.TabIndex = 0;
            this.vScrollBar.ValueChanged += new System.EventHandler(this.vScrollBar_ValueChanged);
            // 
            // TabPageTransmittedReceived
            // 
            this.BackColor = System.Drawing.Color.LightGray;
            this.Controls.Add(this.vScrollBar);
            this.ResumeLayout(false);

        }

        public void uploadData(Collection<Message> messages)
        {
            if (this.messagelist.Count > 0)
            {
                foreach (PanelMessage message in this.messagelist)
                {
                    this.Controls.Remove(message);
                }
            }
            this.messagelist = new List<PanelMessage>();
            int counter = 10;
            foreach (Message message in messages)
            {
                PanelMessage panelMessage = new PanelMessage(message);
                panelMessage.Location = new Point(5, counter);
                this.Controls.Add(panelMessage);
                this.messagelist.Add(panelMessage);
                counter += 30;
            }
            this.messageLength = counter;
            this.Refresh();
        }

        private void vScrollBar_ValueChanged(object sender, EventArgs e)
        {
            if (this.messageLength > 240)
            {
                int messageListLengthTemp = -(this.messageLength * this.vScrollBar.Value) / 100;
                for (int i = 0; i < messagelist.Count; i++)
                {
                    messagelist[i].Location = new Point(5, messageListLengthTemp);

                    messageListLengthTemp += 30;


                }
                this.Refresh();
            }
        }

    }
}
